#include "../main_head.h"
#include "../other_head.h"
#include "Number_left.h"
#include "Number_right.h"
#include "Begin.h"
#include "End.h"

GLboolean here_live, up = 0, start = 0, replay = 0, good = 1;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
	if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
		glfwSetWindowShouldClose(window, GL_TRUE);
	if (key == GLFW_KEY_SPACE && action == GLFW_PRESS && start)
		up = 1;
	if (key == GLFW_KEY_SPACE && action == GLFW_PRESS && !start)
		start = 1;
	if (key == GLFW_KEY_R && action == GLFW_PRESS && here_live == 0 && start == 1)
		replay = 1;
}

void mouse_callback(GLFWwindow* window, int button, int action, int mods) {
	if (button == 0 && action == 1 && !start)
		start = 1;
	if (button == 0 && action == 1 && start)
		up = 1;
}

GLFWwindow* __init__(GLint WIDTH, GLint HEIGHT) {
	glfwInit();

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

	glEnable(GL_DEPTH_TEST);

	GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Flappy Bird", nullptr, nullptr);
	glfwMakeContextCurrent(window);
	glfwSetKeyCallback(window, key_callback);
	glfwSetMouseButtonCallback(window, mouse_callback);
	glewExperimental = GL_TRUE;
	glewInit();

	glViewport(0, 0, WIDTH, HEIGHT);
	return window;
}

void load_all_the_image(BK &ourBK, Bird &ourbird, Pipe &ourpipe, Number_left &ournumber_left,
	Number_right &ournumber_right, Begin &ourbegin, End &ourend) {
	ourbird.load_image(); ourbird.load_coord();
	ourBK.load_image(); ourBK.load_coord();
	ourpipe.load_image(); ourpipe.load_coord();
	ournumber_left.load_coord_and_image(); ournumber_right.load_coord_and_image();
	ourbegin.load_coord_and_image(); ourend.load_coord_and_image();
}

void unbind_all_the_thing(BK &ourBK, Bird &ourbird, Pipe &ourpipe) {
	ourbird.burn(); ourBK.burn(); ourpipe.burn();
}

void draw_the_main_thing_and_judge_whether_you_are_die(Shader &S_BK, BK &ourBK, 
	Shader &S_Bird, Bird &ourbird, Shader &S_Pipe, Pipe &ourpipe, GLboolean &live, GLboolean &start) {
	ourBK.set_live(live); ourbird.set_live(live); ourpipe.set_live(live);
	S_BK.Use(); ourBK.draw(start);
	S_Bird.Use();
	if (live) {
		if(start)ourbird.control(up); ourbird.draw(); up = 0;
		here_live = live = ourbird.judge(ourpipe.pipePositions, ourpipe._control_all());
	} else {
		ourbird.fall(); ourbird.draw();
	}
	S_Pipe.Use(); ourpipe.draw(start);
}

void draw_the_number_and_do_you_get_the_point(Shader &S_Number_Left, Number_left &ournumber_left, 
	Shader &S_Number_Right, Number_right &ournumber_right, Pipe &ourpipe, Bird &ourbird, 
	GLboolean &live) {
	S_Number_Left.Use(); ournumber_left.draw();
	S_Number_Right.Use(); ournumber_right.draw();
	if (live && ourbird.get_the_point(ourpipe.pipePositions, ourpipe._control_all())) {
		good = here_live = live = ournumber_left.change_number(ournumber_right.change_number(1,ournumber_left.get8()=='9'));
		ournumber_left.burn(); ournumber_right.burn();
		ournumber_left.load_coord_and_image(); ournumber_right.load_coord_and_image();
	}
}
void can_we_begin(GLboolean &_start) {
	_start = start;
}

void Check_we_begin_and_check_we_end(Shader &S_Begin, Begin &ourbegin, Shader &S_End,
	End &ourend, GLboolean &live, GLboolean &start) {

	can_we_begin(start);
	if (!start) {
		S_Begin.Use(); ourbegin.draw();
	}
	if (!live) {
		if (!good) {
			ourend.load_image("picture/title.png");
			good = 1;
		}
		S_End.Use(); ourend.draw();
	}
}

GLboolean get_replay() { 
	if (replay) {
		here_live = 1; start = 0; up = 0; replay = 0;
		return true;
	}
	return false;
}

GLboolean if_die(GLboolean &live, GLboolean &start) {
	if (!live && start) if (get_replay()) return true;
	return false;
}